package com.ruoyi.central.controller;

import java.util.List;

import com.ruoyi.common.validation.group.InsertGroup;
import com.ruoyi.common.validation.group.UpdateGroup;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.utils.bean.SelectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.central.domain.TopCentralControllerCommunicationNode;
import com.ruoyi.central.service.ITopCentralControllerCommunicationNodeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.validation.group.UpdateBatchGroup;
import com.ruoyi.common.core.domain.StateVo;
import java.util.Collection;
/**
 * 通信节点Controller
 * 
 * @author ruoyi
 * @date 2022-02-25
 */
@Controller
@RequestMapping("/central/centralControllerCommunicationNode")
public class TopCentralControllerCommunicationNodeController extends BaseController
{
    private String prefix = "central/centralControllerCommunicationNode";

    @Autowired
    private ITopCentralControllerCommunicationNodeService topCentralControllerCommunicationNodeService;

    @RequiresPermissions("central:centralControllerCommunicationNode:view")
    @GetMapping()
    public String centralControllerCommunicationNode()
    {
        return prefix + "/centralControllerCommunicationNode";
    }

    /**
     * 查询通信节点列表
     */
    @RequiresPermissions("central:centralControllerCommunicationNode:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(TopCentralControllerCommunicationNode topCentralControllerCommunicationNode)
    {
        startPage();
        List<TopCentralControllerCommunicationNode> list = topCentralControllerCommunicationNodeService.selectList(topCentralControllerCommunicationNode);
        return getDataTable(list);
    }

    /**
     * 查询简单的通信节点列表 用于select选择器
     */
    @GetMapping("/simpleList")
    @ResponseBody
    public AjaxResult simpleList(TopCentralControllerCommunicationNode topCentralControllerCommunicationNode){

        Collection<TopCentralControllerCommunicationNode> topCentralControllerCommunicationNodeList = topCentralControllerCommunicationNodeService.selectAllMap().values();

        return AjaxResult.success(SelectUtils.buildSelectList(topCentralControllerCommunicationNodeList,TopCentralControllerCommunicationNode::getId,TopCentralControllerCommunicationNode::getNodeTitle));
    }

    /**
     * 获取通信节点详细信息
     */
    @RequiresPermissions("central:centralControllerCommunicationNode:query")
    @GetMapping(value = "/{id}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(topCentralControllerCommunicationNodeService.selectById(id));
    }

    /**
     * 获取自增排序值
     * @param topCentralControllerCommunicationNode 过滤条件
     * @return
     */
    @GetMapping("/getIncrementWeight")
    @ResponseBody
    public AjaxResult getIncrementWeight(TopCentralControllerCommunicationNode topCentralControllerCommunicationNode){
        int weigth = topCentralControllerCommunicationNodeService.getMaxOfWeight(topCentralControllerCommunicationNode);
        return AjaxResult.success(++weigth);
    }

    /**
     * 下载通信节点导入模板
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<TopCentralControllerCommunicationNode> util = new ExcelUtil<>(TopCentralControllerCommunicationNode.class);
        util.importTemplateExcel(response, "通信节点数据");
    }

    /**
     * 导入通信节点数据
     * @param file 通信节点数据文件
     * @param updateSupport 是否为更新
     */
    @Log(title = "通信节点", businessType = BusinessType.IMPORT)
    @RequiresPermissions("central:centralControllerCommunicationNode:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TopCentralControllerCommunicationNode> util = new ExcelUtil<>(TopCentralControllerCommunicationNode.class);
        List<TopCentralControllerCommunicationNode> topCentralControllerCommunicationNodeList = util.importExcel(file.getInputStream());

        if(updateSupport){
            if(topCentralControllerCommunicationNodeService.updateBatchById(topCentralControllerCommunicationNodeList)){
                return AjaxResult.success("导入通信节点数据覆盖成功！");
            }
            return AjaxResult.error("导入通信节点数据覆盖失败！");
        }
        if(topCentralControllerCommunicationNodeService.insertBatch(topCentralControllerCommunicationNodeList)){
            return AjaxResult.success("导入通信节点数据新增成功！");
        }
        return AjaxResult.error("导入通信节点数据新增失败！");
    }

    /**
     * 导出通信节点列表
     */
    @RequiresPermissions("central:centralControllerCommunicationNode:export")
    @Log(title = "通信节点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TopCentralControllerCommunicationNode topCentralControllerCommunicationNode)
    {
        List<TopCentralControllerCommunicationNode> list = topCentralControllerCommunicationNodeService.selectList(topCentralControllerCommunicationNode);
        ExcelUtil<TopCentralControllerCommunicationNode> util = new ExcelUtil<TopCentralControllerCommunicationNode>(TopCentralControllerCommunicationNode.class);
        return util.exportExcel(list, "通信节点数据");
    }

    /**
     * 新增通信节点
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存通信节点
     */
    @RequiresPermissions("central:centralControllerCommunicationNode:add")
    @Log(title = "通信节点", businessType = BusinessType.INSERT)
    @PostMapping
    @ResponseBody
    public AjaxResult addSave(@Validated(InsertGroup.class) @RequestBody TopCentralControllerCommunicationNode topCentralControllerCommunicationNode)
    {
        return toAjax(topCentralControllerCommunicationNodeService.insert(topCentralControllerCommunicationNode));
    }

    /**
     * 修改通信节点
     */
    @RequiresPermissions("central:centralControllerCommunicationNode:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        TopCentralControllerCommunicationNode topCentralControllerCommunicationNode = topCentralControllerCommunicationNodeService.selectById(id);
        mmap.put("topCentralControllerCommunicationNode", topCentralControllerCommunicationNode);
        return prefix + "/edit";
    }

    /**
     * 修改保存通信节点
     */
    @RequiresPermissions("central:centralControllerCommunicationNode:edit")
    @Log(title = "通信节点", businessType = BusinessType.UPDATE)
    @PutMapping
    @ResponseBody
    public AjaxResult editSave(@Validated(UpdateGroup.class) @RequestBody TopCentralControllerCommunicationNode topCentralControllerCommunicationNode)
    {
        return toAjax(topCentralControllerCommunicationNodeService.updateById(topCentralControllerCommunicationNode));
    }

    /**
     * 修改通信节点 状态 根据 ids 主键
     */
    @RequiresPermissions("central:centralControllerCommunicationNode:edit")
    @Log(title = "通信节点", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStateByIds")
    @ResponseBody
    public AjaxResult updateStateByIds(@Validated(value= UpdateBatchGroup.class)@RequestBody StateVo<Integer> stateVo)
    {
        return toAjax(topCentralControllerCommunicationNodeService.updateStateByIds(stateVo.getIds(),stateVo.getState()));
    }

    /**
     * 恢复通信节点 为未删除 根据 ids 主键
     */
    @RequiresPermissions("central:centralControllerCommunicationNode:restore")
    @Log(title = "通信节点", businessType = BusinessType.UPDATE)
    @PutMapping("/restoreData/{ids}")
    @ResponseBody
    public AjaxResult recoverByIds(@PathVariable("ids") List<Integer> ids){
        return toAjax(topCentralControllerCommunicationNodeService.recoverBatchByIds(ids));
    }

    /**
     * 删除通信节点
     */
    @RequiresPermissions("central:centralControllerCommunicationNode:remove")
    @Log(title = "通信节点", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ResponseBody
    public AjaxResult deleteByIds(@PathVariable("ids") List<Integer> ids)
    {
            return toAjax(topCentralControllerCommunicationNodeService.logicDeleteBatchByIds(ids));
    }
}
